package YunNotes.allnotes.dao;

import YunNotes.allnotes.model.NotesUserPublic;
import YunNotes.util.db.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserPublicNotes {
    public ArrayList<NotesUserPublic> outputUserPublicNotes(int num) {
        DBUtil obj = new DBUtil();
        Connection conn = DBUtil.getConn();
        ArrayList<NotesUserPublic> users = new ArrayList<>();
//        ArrayList<NotesUserPublic> userList = new ArrayList<>();
        if (conn == null) {
            System.out.println("数据库连接出错");
            return null;
        }
        String sql = "SELECT * from allnotes where num=?";
        try {
            //预编译处理sql语句
            PreparedStatement ptst = conn.prepareStatement(sql);
            ptst.setInt(1, num);
            ResultSet rs = ptst.executeQuery();
            rs.last();
            rs.beforeFirst();
            while (rs.next()) {
                NotesUserPublic user = new NotesUserPublic();
                user.setNum(rs.getInt("num"));
                user.setUserName(rs.getString("user_name"));
                user.setHeadLine(rs.getString("head_line"));
                user.setContent(rs.getString("content"));
                user.setPraisePoints(rs.getInt("praise_points"));
                user.setPageView(rs.getInt("page_view"));
                user.setCreateTime(rs.getTimestamp("create_time"));
                users.add(user);
            }
//            obj.closeConn(conn);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQL语句有误，数据库报错");
            return null;
        }

//        userList.add(users.get(i));

        return users;
    }
}
